package com.srwl.mytx.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.srwl.easeui.utils.TimeInfo;
import com.srwl.mytx.MyApplication;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

/**
 * 时间工具类
 */
@SuppressLint("SimpleDateFormat")
public class DateUtils {

    private static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    private static final String FORMAT_MM_DD = "MM-dd";
    private static final String FORMAT_HH_MM = "HH:mm";
    public static final String FORMAT_TIMESTAMP = "yyyy-MM-dd HH:mm:ss";
    public static long currentTime;

    private static int getNowYear() {
        Calendar mCalendar = Calendar.getInstance();
        return mCalendar.get(Calendar.YEAR);
    }

    private static int getNowMonth() {
        Calendar mCalendar = Calendar.getInstance();
        return mCalendar.get(Calendar.MONTH);
    }

    private static int getNowDay() {
        Calendar mCalendar = Calendar.getInstance();
        return mCalendar.get(Calendar.DAY_OF_MONTH);
    }

    private static int getOldYear(Calendar mCalendar) {
        return mCalendar.get(Calendar.YEAR);
    }

    private static int getOldMonth(Calendar mCalendar) {
        return mCalendar.get(Calendar.MONTH);
    }

    private static int getOldDay(Calendar mCalendar) {
        return mCalendar.get(Calendar.DAY_OF_MONTH);
    }

    private static boolean isToday(Calendar mCalendar) {
        return getNowYear() == getOldYear(mCalendar) && getNowMonth() == getOldMonth(mCalendar) && getNowDay() == getOldDay(mCalendar);
    }

    private static boolean isYesterday(Calendar mCalendar) {
        return getNowYear() == getOldYear(mCalendar);
    }

    /**
     * 字符串日期转成Date
     *
     * @param dateStr 日期字符串
     * @return Date
     */
    private static Date stringToDate(String dateStr) {
        SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_TIMESTAMP);
        try {
            return formatter.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date stringToDate(String dateStr, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
            return formatter.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * Date转成格式化日期字符串
     *
     * @param date       Date
     * @param dateFormat 日期格式
     * @return 对应格式的时间
     */
    public static String dateToString(Date date, String dateFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(date);
    }

    /**
     * 获取格式化日期
     *
     * @param time 字符串日期
     * @return 字符串格式的时间
     */
    public static String getDateFormat(String time) {
        if (TextUtils.isEmpty(time)) {
            return "";
        }
        Date date = stringToDate(time);
        if (date == null) {
            return time;
        }
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTime(date);
        if (isToday(mCalendar)) {
            return dateToString(date, FORMAT_HH_MM);

        } else if (isYesterday(mCalendar)) {
            return dateToString(date, FORMAT_MM_DD);
        } else {
            return dateToString(date, FORMAT_YYYY_MM_DD);
        }

    }

    /**
     * 计算 hour 小时后的时间
     *
     * @param date
     * @param hour
     * @return
     */
    public static Date addHour(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, hour);
        return calendar.getTime();
    }

    public static Date addDays(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

    public static String getTimestampString(Date var0) {
        if (var0 == null) {
            return "";
        }
        String dateFormat = null;
        String languageLocal = Locale.getDefault().getLanguage();
        boolean isChinese = languageLocal.startsWith("zh");
        long time = var0.getTime();
        if (isChinese) {
            if (isSameDay(time)) {
                dateFormat = "今天 HH:mm";
            } else if (isYesterday(time)) {
                dateFormat = "昨天 HH:mm";
            } else if (isThisYear(time)) {
                dateFormat = "M月d日 HH:mm";
            } else {
                dateFormat = "yyyy年MM月dd日";
            }
        } else {
            if (isSameDay(time)) {
                return (new SimpleDateFormat("HH:mm", Locale.ENGLISH)).format(var0) + " Today";
            } else if (isYesterday(time)) {
                return (new SimpleDateFormat("HH:mm", Locale.ENGLISH)).format(var0) + " Yesterday";
            } else if (isThisYear(time)) {
                dateFormat = "MMM,dd";
            } else {
                dateFormat = "MMM,dd,yyyy";
            }
        }
        return isChinese ? (new SimpleDateFormat(dateFormat, Locale.CHINESE)).format(var0) : (new SimpleDateFormat(dateFormat, Locale.ENGLISH)).format(var0);
    }

    /**
     * 格式化Date为字符串，采用"...前"的形式
     */
    public static String getDateToString(Date date) {
        String languageLocal = Locale.getDefault().getLanguage(); //当前系统操作环境语言
        boolean isChinese = languageLocal.startsWith("zh");
        if (date == null) {
            if (isChinese) {
                return "刚刚";    //由于comment的创建时间是在后端设置的，如果只是在adapter中刷新刚刚加入的数据就并没有创建时间，所以可以判断为刚刚加入的数据
            } else {
                return "just now";
            }
        }
        String dateFormat = null; //时间格式
        long time = date.getTime();
        if (isChinese) {
            currentTime = System.currentTimeMillis() + MyApplication.timeOffset;
            if (notMoreThanMinute(time)) {
                //一分钟以内
                time = currentTime - time;
                if (time < 10 * 1000) {
                    return "刚刚";
                } else {
                    dateFormat = "ss秒前";
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
                simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));    //如果不设置时区则会相差8小时
                return simpleDateFormat.format(time);
            } else if (notMoreThanHour(time)) {
                time = currentTime - time;
                if (time < 10 * 60 * 1000) {
                    dateFormat = "m分钟前";
                } else {
                    dateFormat = "mm分钟前";
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
                simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
                return simpleDateFormat.format(time);
            } else if (notMoreThanDay(time)) {
                time = currentTime - time;
                if (time < 10 * 60 * 60 * 1000) {
                    dateFormat = "H小时前";
                } else {
                    dateFormat = "HH小时前";
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
                simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
                return simpleDateFormat.format(time);
            } else if (isYesterday(time)) {
                dateFormat = "昨天 HH:mm";
            } else if (isThisYear(time)) {
                dateFormat = "MM-dd";
            } else {
                dateFormat = "yyyy-MM-dd";
            }
        } else {
            if (isSameDay(time)) {
                dateFormat = "HH:mm";
                return (new SimpleDateFormat(dateFormat, Locale.ENGLISH)).format(date) + " Today";
            } else if (isYesterday(time)) {
                dateFormat = "HH:mm";
                return (new SimpleDateFormat(dateFormat, Locale.ENGLISH)).format(date) + " Yesterday";
            } else if (isThisYear(time)) {
                dateFormat = "MMM,dd";
            } else {
                dateFormat = "MMM,dd,yyyy";
            }
        }

        return isChinese ? (new SimpleDateFormat(dateFormat, Locale.CHINESE)).format(date) : (new SimpleDateFormat(dateFormat, Locale.ENGLISH)).format(date);
    }


    public static boolean isCloseEnough(long var0, long var2) {
        long var4 = var0 - var2;
        if (var4 < 0L) {
            var4 = -var4;
        }

        return var4 < 30000L;
    }

    private static boolean notMoreThanMinute(long var0) {
        return currentTime - var0 < 60 * 1000 ? true : false;
    }

    private static boolean notMoreThanHour(long var0) {
        return currentTime - var0 < 60 * 60 * 1000 ? true : false;
    }

    private static boolean notMoreThanDay(long var0) {
        return currentTime - var0 < 60 * 60 * 24 * 1000 ? true : false;
    }

    private static boolean isSameDay(long var0) {
        TimeInfo var2 = getTodayStartAndEndTime();
        return var0 > var2.getStartTime() && var0 < var2.getEndTime();
    }

    private static boolean isYesterday(long var0) {
        TimeInfo var2 = getYesterdayStartAndEndTime();
        return var0 > var2.getStartTime() && var0 < var2.getEndTime();
    }

    private static boolean isThisYear(long var0) {
        TimeInfo var2 = getThisYearStartAndEndTime();
        return var0 > var2.getStartTime() && var0 < var2.getEndTime();
    }


    public static TimeInfo getTodayStartAndEndTime() {
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.set(Calendar.HOUR_OF_DAY, 0);
        calendarStart.set(Calendar.MINUTE, 0);
        calendarStart.set(Calendar.SECOND, 0);
        calendarStart.set(Calendar.MILLISECOND, 0);
        long var1 = calendarStart.getTimeInMillis() + MyApplication.timeOffset;
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.set(Calendar.HOUR_OF_DAY, 23);
        calendarEnd.set(Calendar.MINUTE, 59);
        calendarEnd.set(Calendar.SECOND, 59);
        calendarEnd.set(Calendar.MILLISECOND, 999);
        long var2 = calendarEnd.getTimeInMillis() + MyApplication.timeOffset;
        TimeInfo var3 = new TimeInfo();
        var3.setStartTime(var1);
        var3.setEndTime(var2);
        return var3;
    }

    public static TimeInfo getYesterdayStartAndEndTime() {
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.add(Calendar.DAY_OF_MONTH, -1);
        calendarStart.set(Calendar.HOUR_OF_DAY, 0);
        calendarStart.set(Calendar.SECOND, 0);
        calendarStart.set(Calendar.MINUTE, 0);
        calendarStart.set(Calendar.MILLISECOND, 0);
        Date var1 = calendarStart.getTime();
        long var2 = var1.getTime() + MyApplication.timeOffset;
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.add(Calendar.DAY_OF_MONTH, -1);
        calendarEnd.set(Calendar.HOUR_OF_DAY, 23);
        calendarEnd.set(Calendar.MINUTE, 59);
        calendarEnd.set(Calendar.SECOND, 59);
        calendarEnd.set(Calendar.MILLISECOND, 999);
        Date var5 = calendarEnd.getTime();
        long var6 = var5.getTime() + MyApplication.timeOffset;
        TimeInfo var8 = new TimeInfo();
        var8.setStartTime(var2);
        var8.setEndTime(var6);
        return var8;
    }

    public static TimeInfo getThisYearStartAndEndTime() {
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.set(calendarStart.get(Calendar.YEAR), 0, 1, 0, 0, 0);
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.set(calendarEnd.get(Calendar.YEAR), 11, 31, 23, 59, 59);
        TimeInfo timeInfo = new TimeInfo();
        timeInfo.setStartTime(calendarStart.getTimeInMillis() + MyApplication.timeOffset);
        timeInfo.setEndTime(calendarEnd.getTimeInMillis() + MyApplication.timeOffset);
        return timeInfo;
    }

    @SuppressLint({"SimpleDateFormat"})
    public static Date StringToDate(String var0, String var1) {
        SimpleDateFormat var2 = new SimpleDateFormat(var1);
        Date var3 = null;

        try {
            var3 = var2.parse(var0);
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        return var3;
    }

    @SuppressLint({"DefaultLocale"})
    public static String toTime(int var0) {
        var0 /= 1000;
        int var1 = var0 / 60;
        boolean var2 = false;
        if (var1 >= 60) {
            int var4 = var1 / 60;
            var1 %= 60;
        }

        int var3 = var0 % 60;
        return String.format("%02d:%02d", var1, var3);
    }

    @SuppressLint({"DefaultLocale"})
    public static String toTimeBySecond(int var0) {
        int var1 = var0 / 60;
        boolean var2 = false;
        if (var1 >= 60) {
            int var4 = var1 / 60;
            var1 %= 60;
        }

        int var3 = var0 % 60;
        return String.format("%02d:%02d", var1, var3);
    }

    /**
     * @param startDate 起始日期
     * @param timeout   有效期
     * @param unit
     * @return
     */
    public static boolean isExpired(Date startDate, long timeout, TimeUnit unit) {

        if (startDate == null) {
            return true;
        }
        long startDateTime = startDate.getTime();
        long currentDateTime = new Date().getTime();

        long millis = unit.toMillis(timeout);

        return currentDateTime - startDateTime > millis;
    }

    public static String getTimeSpanWithMillis(Date startDate, Date endDate) {


        long startDateTime = startDate.getTime();
        long endDateTime = endDate.getTime();
        long diff = endDateTime - startDateTime;
        //  return TimeUnit.MILLISECONDS.toMillis(endDateTime - startDateTime);
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        long second = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
        if (days != 0) {
            return +days + "天" + hours + "小时" + minutes + "分";
        } else if (hours != 0) {
            return +hours + "小时" + minutes + "分";
        } else if (minutes != 0) {
            return +minutes + "分" + second + "秒";
        } else {
            return +second + "秒";
        }
    }

    public static String computeDurationTime(Date startDate, Date endDate) {
        long startDateTime = startDate.getTime();
        long endDateTime = endDate.getTime();
        long diff = endDateTime - startDateTime;
        return computeDurationTime(diff);
    }

    @NonNull
    public static String computeDurationTime(long milliSeconds) {
        long days = milliSeconds / (1000 * 60 * 60 * 24);
        long hours = (milliSeconds - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (milliSeconds - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        long second = (milliSeconds - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
        String duration = "";
        if (days != 0) {
            duration += days + "天";
        }
        if (hours != 0) {
            duration += hours + "小时";
        }
        if (minutes != 0) {
            duration += minutes + "分钟";
        }
        if (second != 0) {
            duration += second + "秒";
        }
        return duration;
    }

}
